<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/12
 * Time: 15:40
 */

namespace App\HttpController\V1;

use App\Common\Qiniuyun;
use App\HttpController\Taoran\Functions\Publics;
class XuanOperate extends Auth
{

    /**
     * 上传短视频
     * @return bool
     */
    function upload(){
        $images = $this->input( 'images' );
        $video=$this->input( 'video' );
        $img=$this->input( 'video_img' );
        $title=$this->input('title');
        $widths=$this->input('widths');
        $heights=$this->input('heights');
        if($images&&$video){
            return $this->writeJson(-202,null,'只能上传图片或者视频其中一种');
        }

        $data=[
            'user_id'=>$this->user->id,
            'class_id'=>47,
            'study_title'=>$title,
            'create_time'=>date('Y-m-d H:i:s')
        ];
        if($images){
            $data['image']=$images;
        }else{
            $data['vip_video']=$video;
            $data['video_img']=$img;
            $data['widths']=$widths;
            $data['heights']=$heights;
        }
        try{
            $this->db->insert('study',$data);
            return $this->writeJson(200,null,'成功');
        }catch (\Exception $e){
            return $this->writeJson(-201,null,$e->getMessage());
        }
    }



    /**
     * 我的关注
     */
    function my_follow(){
        $page=$this->getPage();
        $Publics= new Publics();
        $user_id=$this->user->id;
        $class_id=$this->db->where('pid',2)->getValue('study_class','id');
        $follow=$this->db->where('user_id',$user_id)->get('users_follow',null,'to_user_id');

        $arr_userid = array_column($follow, 'to_user_id');//关注user_id

        $data=[];
        if($arr_userid){
            $data=$this->db
                ->whereIn('user_id',$arr_userid)
                ->where('class_id',$class_id)
                ->orderBy('id','desc')
                ->get('study',$page,"id,study_title,user_id,image,vip_video,`like`,favorite,comm_num,create_time,video_img,widths,heights,browse");

            foreach ($data as $k=>&$v){
                if($v['image']){
                    $images=explode(';',$v['image']);
                    foreach($images as &$val){
                        $val=QINIU_URL.$val;
                    }
                    $v['image']=$images;
                }else{
                    $v['image']=[];
                }
                if($v['vip_video']){
                    $v['vip_video']=QINIU_URL.$v['vip_video'];
                    $v['video_img']=QINIU_URL.$v['video_img'];
                }
                $v['create_time']=$Publics->get_last_time($v['create_time']);
                //判断收藏 关注 点赞
                $v['is_follow']=$this->db->where('user_id',$user_id)->where('to_user_id',$v['user_id'])->has('users_follow')?1:0;
                $v['is_like']=$this->db->where('user_id',$user_id)->where('study_id',$v['id'])->has('study_like')?1:0;
                $v['is_collect']=$this->db->where('user_id',$user_id)->where('study_id',$v['id'])->has('users_collection')?1:0;
                $users=$this->db->where('id',$v['user_id'])->getOne('users','nickname,headimg');
                $v['nickname']=$users['nickname'];
                $v['headimg']=QINIU_URL.$users['headimg'];
                $v['comment']=$this->db->where('study_id',$v['id'])->whereNull('to_user_id')->orderBy('id')->get('study_comment',3,'id,user_id,content');
                if($v['comment']){
                    foreach ($v['comment'] as $ks=>&$vs){

                        $nickname=$this->db->where('id',$vs['user_id'])->getValue('users','nickname');
                        $vs['user_nickname']=$nickname;
                    }
                }
            }
        }
        return $this->writeJson(200,$data,'成功');
    }





}